import { EventCode, getMouseInfo } from "./utiles"
import _ from 'lodash'
class edittools {

  constructor() {
    this._viewer = window.viewer
    this.scene = window.viewer.scene
    // this._options = options
    this._layer = new Cesium.CustomDataSource('editerlayer')
    this._viewer.dataSources.add(this._layer)
    this._editpoints = []
    this._handler = new Cesium.ScreenSpaceEventHandler(
      viewer.scene.canvas,
    )

  }

  toggleCameraController(status) {
    this.scene.screenSpaceCameraController.enableRotate = status
    // 如果为true，则允许用户平移地图。如果为假，相机将保持锁定在当前位置。此标志仅适用于2D和Columbus视图模式。
    this.scene.screenSpaceCameraController.enableTranslate = status
    // 如果为真，允许用户放大和缩小。如果为假，相机将锁定到距离椭圆体的当前距离
    this.scene.screenSpaceCameraController.enableZoom = status
    // 如果为真，则允许用户倾斜相机。如果为假，相机将锁定到当前标题。这个标志只适用于3D和哥伦布视图。
    this.scene.screenSpaceCameraController.enableTilt = status
  }

  edit(e) {

    // 第一步 清除之前编辑列表
    this.editend()
    this._editpoints = e.createPoints()
    console.log(this._editpoints);
    this._editpoints.map(item => {
      this._layer.entities.add(item.entity)
    })
    this.editstart(e)
    this._handler
  }

  editstart(e) {

    let that = this

    let currentKeyPoint = null

    function moveaction(movement) {
      //设置两个坐标
      console.log(currentKeyPoint);
      // currentKeyPoint.positions[]

      currentKeyPoint.setPositions(getMouseInfo(movement.endPosition).surfacePosition)
      let eindex = currentKeyPoint.options.id.split("|")[2]
      e.positions[eindex] = getMouseInfo(movement.endPosition).surfacePosition
      

    }

    function leftUpaction() {
      that._handler.removeInputAction(EventCode['mouseMove'])
      setTimeout(() => {
        
        that.toggleCameraController(true)
      }, 200);
    }
    function leftDownaction(movement) {
      console.log(11111);
      const picked = that.scene.pick(movement.position, 3, 3)
      console.log(picked);
      let editpoint = null
      if (picked && picked.id) {
        that._editpoints.map(item => {
          item.entity == picked.id ? editpoint = item : 1
        })
        if (editpoint) {
          editpoint.color = '#0f0'
          currentKeyPoint = editpoint
          that.toggleCameraController(false)
          that._handler.setInputAction(moveaction, EventCode['mouseMove'])
          that._handler.setInputAction(leftUpaction, EventCode['leftUp'])
        }

      }
    }


    this._handler.setInputAction(leftDownaction, EventCode['leftDown'])
  }

  editend() {
    this._layer.entities.removeAll()
    if ( _.has(window, 'editend') ) {
      window.editend()
    }
  }


}

export default edittools